//
//  AutoDisapearWindowController.m
//  ToolbarTest
//
//  Created by Junjie Gong on 12-5-28.
//  Copyright (c) 2012年 nhn. All rights reserved.
//

#import "AutoDisapearWindowController.h"
#include <string>

struct UserInfor
{
    bool permission;
    std::string path;
};

@implementation AutoDisapearWindowController

@synthesize contentView;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)dealloc{
    [contentView release];
    [super dealloc];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    [self.window.contentView addSubview:contentView];
    
    [self.window setBackgroundColor:[NSColor clearColor]];
    [self.window setOpaque:NO];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

- (void)startShowWindow{
    [self showWindow:nil];
    
    
    eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSKeyDownMask handler:^(NSEvent *incomingEvent) {
        NSEvent *result = incomingEvent;
        NSWindow *targetWindowForEvent = [incomingEvent window];
        if (targetWindowForEvent != self.window) {
            [self.window close];
        } 
        else if ([incomingEvent type] == NSKeyDown) {
            if ([incomingEvent keyCode] == 53) {
                // Escape
                [self.window close];
                result = nil; // Don't process the event
            }
        }
        return result;
    }];
}

- (void)windowWillClose:(NSNotification *)notification{
    if (eventMonitor) {
        [NSEvent removeMonitor:eventMonitor];
        eventMonitor = nil;
    }
    [self autorelease];
}

- (IBAction)stopWindow:(id)sender{
    [self.window close];
}

@end
